package com.base.library.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.base.library.R;


public class TabMenuView extends LinearLayout {
    private ImageView imageView;
    private TextView textView;
    private TextView textUnread;//显示未读条数

    public TabMenuView(Context context) {
        this(context, null);
    }

    public TabMenuView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context, attrs);
    }

    public TabMenuView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initView(context, attrs);
    }

    public void initView(Context context, AttributeSet attrs) {
        if (!isInEditMode()) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View view = inflater.inflate(R.layout.tab_menu_view, this);
            imageView = (ImageView) view.findViewById(R.id.img_icon);
            textView = (TextView) view.findViewById(R.id.tv_name);
            textUnread = (TextView) view.findViewById(R.id.unread_msg_number);
            TypedArray typeArray = context.obtainStyledAttributes(attrs, R.styleable.TabMenuView);
            imageView.setImageResource(typeArray.getResourceId(R.styleable.TabMenuView_imgIcon, 0));
            textView.setText(getResources().getString(typeArray.getResourceId(R.styleable.TabMenuView_textName, R.string.str_empty)));
            textView.setTextColor(typeArray.getColor(R.styleable.TabMenuView_textColor, context.getResources().getColor(R.color.black3)));
            typeArray.recycle();
        }
    }

    public void setTextUnread(int count) {
        if (count > 99) {
            textUnread.setVisibility(VISIBLE);
            textUnread.setText("99+");
        } else if (count > 0) {
            textUnread.setVisibility(VISIBLE);
            textUnread.setText("" + count);
        } else {
            textUnread.setVisibility(GONE);
        }
    }


    @Override
    public void setEnabled(boolean enabled) {
        imageView.setEnabled(enabled);
        textView.setEnabled(enabled);
        super.setEnabled(enabled);
    }
}
